package priv.ln.search_sort;

/**
 * ClassName: exer1
 * Package: priv.ln.search_sort
 * Description:二分法查找
 *
 * 定义数组：int[] arr2 = new int[]{2,4,5,8,12,15,19,26,37,49,51,66,89,100};
 * 查找元素5是否在上述数组中出现过？如果出现，输出对应的索引值。
 *
 * @Author 辣辣
 * @Create 2024/6/14 17:56
 * @Version 1.0
 */
public class exer1 {
    public static void main(String[] args) {
        int[] arr = new int[]{2,4,5,8,12,15,19,26,37,49,51,66,89,100};
        int target = 5;
        boolean isFound = false;

        int head = 0;
        int end = arr.length - 1;

        while (head <= end) {
            int mid = (head + end) / 2;
            if(target == arr[mid]) {
                isFound = true;
                System.out.println("找到了，位置为：" + mid);
                break;
            } else if (target < arr[mid]) {
                end = mid - 1;
            }else{
                head = mid + 1;
            }
        }

        if(!isFound){
            System.out.println("没找到");
        }
    }
}
